package com.aoxon.cargo.component;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.activity.PurNoteInputActivity;
import com.aoxon.cargo.bean.Note;
import com.aoxon.cargo.shared.NoteShared;
import com.aoxon.cargo.view.MyScrollView;
import com.google.gson.Gson;

/**
 * 便签记录的瀑布流显示
 * 
 * @author ZZJ
 * 
 */
public class WaterFallShowNote implements MyScrollView.OnScrollListener {

	private Activity activity;

	private MyScrollView myScrollView;
	private MyAlertDialog myAlertDialog;
	
	private LinearLayout llContainer;
	private RelativeLayout rlListBootom;
	private ArrayList<LinearLayout> linearLayouts = new ArrayList<LinearLayout>();

	private int toAdd = 0;// 数据最新显示的列
	private int item_width;
	private List<Note> notes = new ArrayList<Note>();
	private int layoutColumn = 2; // 设置多少列

	private Gson gson = new Gson();
	
	public WaterFallShowNote(Activity activity, int layoutColumn) {
		
		this.activity = activity;
		this.layoutColumn = layoutColumn;
		
		myScrollView = (MyScrollView) activity
				.findViewById(R.id.msvPubWaterFallList);

		llContainer = (LinearLayout) activity
				.findViewById(R.id.llPubWaterFallContainer);
		rlListBootom = (RelativeLayout) activity
				.findViewById(R.id.rlListBottom);
		
		myScrollView.getView();
		myScrollView.setOnScrollListener(this);
		rlListBootom.setVisibility(View.GONE);

		myAlertDialog = new MyAlertDialog(activity);
		
		item_width = activity.getWindowManager().getDefaultDisplay().getWidth()
				/ layoutColumn;

		for (int i = 0; i < layoutColumn; i++) {
			LinearLayout layout = new LinearLayout(activity.getBaseContext());
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					item_width, LayoutParams.WRAP_CONTENT);
			layout.setOrientation(LinearLayout.VERTICAL);
			layout.setLayoutParams(params);
			linearLayouts.add(layout);
			llContainer.addView(layout);
		}

	}

	public void setNoteList(List<Note> notes) {
		this.notes = notes;
	}

	public void addNote() {
		if (notes != null) {
			for (int i = notes.size() -1; i >= 0 ; i--) {
				getNoteView(notes.get(i), toAdd, i);
				toAdd++;

				if (toAdd >= layoutColumn) {
					toAdd = 0;
				}
			}
		}

	}

	private void getNoteView(Note note, int toLayout, final int number) {

		final View viewItem = activity.getLayoutInflater().inflate(
				R.layout.pur_note_list_item, null);
		final TextView tvTitle = (TextView) viewItem
				.findViewById(R.id.tvNoteListItemTitle);
		final TextView tvTime = (TextView) viewItem
				.findViewById(R.id.tvNoteListItemTime);
		if( number % 5 == 0){
			tvTitle.setTextSize(26);
		}
		
		if(number % 3 == 0){
			tvTitle.setTextSize(15);
		}
		tvTitle.setText(note.getTitle());
		tvTime.setText(note.getTime());
		
		viewItem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PurNoteInputActivity.isNewAdd = false;
				Intent intent = new Intent(activity, PurNoteInputActivity.class);
				intent.putExtra("strNote", gson.toJson(notes.get(number)));
				activity.startActivity(intent);
				activity.finish();
			}
		});
		
		
		viewItem.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				myAlertDialog.show("是否删除？", new MyCallBack() {
					@Override
					public void callback(String choose) {
						// TODO Auto-generated method stub
						notes.remove(number);
						NoteShared.saveNoteList(notes);
						clearView();
						notes = NoteShared.getNoteList();
						addNote();
					}
				});
				
				return false;
			}
		});
		linearLayouts.get(toLayout).addView(viewItem);
	}

	/**
	 * 清空所有列中的子项显示
	 */
	public void clearView() {
		toAdd = 0;
		notes.clear();
		for (int i = 0; i < linearLayouts.size(); i++) {
			linearLayouts.get(i).removeAllViews();
		}
	}

	@Override
	public void onBottom() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll() {
		// TODO Auto-generated method stub

	}
	
	// 即将到达底部（2/3处）
	@Override
	public void onWillToBottom() {
		
	}

}
